# Replication for study 2, Table 5
# Krawatzek / Soroka
# Defending History? The Impact of Context and Speaker in Russia
# Perspectives on Politics
# DOI

require(marginaleffects)
require(MASS)
require(stargazer)
setwd("Insert your local directory")
load("RegressionDataStudy2.RData")


MG1a <- polr(factor(G1_1AllGroups) ~ G1Law + G1Person + GroupCritical + GroupRevisionist + GroupPoliticallyLoyal + GroupNoClearView + C2HistoricalInterest, 
             data=RegressionData)
MG1b <- polr(factor(G1_1AllGroups) ~ G1Law + G1Person + GroupCritical + GroupRevisionist + GroupPoliticallyLoyal + GroupNoClearView + C2HistoricalInterest +
               A1Age + c(A1Age*A1Age) + A2GenderMale + A3Capital + A12SectorEconomyPublicPrivate + A18Education + AWealth, 
             data=RegressionData)
MG2a <- polr(factor(G1_2AllGroups) ~ G1Law + G1Person + GroupCritical + GroupRevisionist + GroupPoliticallyLoyal + GroupNoClearView + C2HistoricalInterest, 
             data=RegressionData)
MG2b <- polr(factor(G1_2AllGroups) ~ G1Law + G1Person + GroupCritical + GroupRevisionist + GroupPoliticallyLoyal + GroupNoClearView + C2HistoricalInterest +
               A1Age + c(A1Age*A1Age) + A2GenderMale + A3Capital + A12SectorEconomyPublicPrivate + A18Education + AWealth, 
             data=RegressionData)
Models <- list(MG1a, MG1b, MG2a, MG2b)

stargazer(Models, p.auto=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels   = c("Appropriate Statement", "Appropriate Statement", "Personal Agreement", "Personal Agreement"),model.names=TRUE,
          digits=3, type="html", title = "Russia Vignette Experiment",
          out="Table 5.doc")

mfx <- marginaleffects(MG1b, type = "probs", variables = c("G1Law","G1Person", "GroupCritical", "GroupRevisionist", "GroupPoliticallyLoyal", "GroupNoClearView"))
mfxsummary <- summary(mfx)
mfxsummary$estimate <- round(mfxsummary$estimate, digits = 4)
mfxsummary$std.error <- round(mfxsummary$std.error, digits = 4)
mfxsummary$statistic <- round(mfxsummary$statistic, digits = 4)
mfxsummary$p.value <- round(mfxsummary$p.value, digits = 4)
mfxsummary$conf.low <- round(mfxsummary$conf.low, digits = 4)
mfxsummary$conf.high <- round(mfxsummary$conf.high, digits = 4)
write.xlsx(mfxsummary, "AME Study 2_Appropriate statement with controls.xlsx")

mfx <- marginaleffects(MG2b, type = "probs", variables = c("G1Law","G1Person", "GroupCritical", "GroupRevisionist", "GroupPoliticallyLoyal", "GroupNoClearView"))
mfxsummary <- summary(mfx)
mfxsummary$estimate <- round(mfxsummary$estimate, digits = 4)
mfxsummary$std.error <- round(mfxsummary$std.error, digits = 4)
mfxsummary$statistic <- round(mfxsummary$statistic, digits = 4)
mfxsummary$p.value <- round(mfxsummary$p.value, digits = 4)
mfxsummary$conf.low <- round(mfxsummary$conf.low, digits = 4)
mfxsummary$conf.high <- round(mfxsummary$conf.high, digits = 4)
write.xlsx(mfxsummary, "AME Study 2_Personal agreement with controls.xlsx")
